import {GetAll} from '@/api/productCover'

interface TechLineBodyItem {
  CreateBy: number;
  CreateTime: string;
  Description: string;
  Dto_TechType?: any;
  IsDelete: number;
  IsEnable: number;
  LastModifyBy: number;
  LastModifyTime: string;
  Name: string;
  NeedTime: number;
  OrderTechRoutes: any[];
  ProductMatetials: any[];
  Reworks: any[];
  TechGuides: any[];
  TechId: number;
  TechLinkMethods: any[];
  TechRouteLinkTeches: any[];
  TechTypeId: number;
  UnifiedTech?: any;
  UserLineWorkstations: any[];
  WorkStationLinkedTeches: any[];
  WorkerOrder: number;
}

let TechIdList = []
let TechIdMap = {}
;(async () => {
  const {Result, Code} = await GetAll();
  TechIdList = Result;
  TechIdMap = TechIdList.reduce<{}>((acc, cur: TechLineBodyItem) => ({...acc, [cur.Name]: cur}), {})
})()

//  获取超时
export function getTechIdByName(name, loop = 10) {
  return new Promise((resolve, reject) => {
    if (TechIdList.length <= 0 && loop <= 0) {
      reject('获取超时')
    }
    if (!TechIdList.length) {
      setTimeout(async () => {
        resolve(await getTechIdByName(name, --loop))
      }, 1000)
    } else {
      return resolve(TechIdMap[name])
    }
  })
}

export const getTechIdByNameSync = (name): TechLineBodyItem => TechIdMap[name]
